import http from '../../util/request.js';
var page = 1;
var pageSize = 20;
var app = getApp();

var GetList = function (that) {
  var categoryId = that.data.categoryId;
  if (that.data.hidden) {
    that.setData({
      hidden: false
    });
    http.get('article', {
      act: 'list',
      page: page,
      pageSize: pageSize,
      categoryId: categoryId
    }).then(function (res) {
      var list = that.data.list;
      if (res.length > 0) {
        if (page == 1) {
          list = res;
        } else {
          for (var i = 0; i < res.length; i++) {
            list.push(res[i]);
          }
        }
        that.setData({
          list: list
        });
        page++;
        that.setData({
          hidden: true
        });
      } else {
        that.setData({
          load_text: '没有更多数据'
        });
      }
    });
  }
}
Page({
  data: {
    hidden: true,
    list: [],
    scrollTop: 0,
    scrollHeight: 0,
    load_text: '加载中...'
  },
  onLoad: function (options) {
    var that = this;
    that.setData({
      categoryId: options.id
    });
    that.getSetting();
    that.getCategoryInfo(options.id);
    wx.getSystemInfo({
      success: function (res) {
        console.info(res.windowHeight);
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });
  },
  onShow: function () {
    var that = this;
    page = 1;
    GetList(that);
  },
  bindDownLoad: function () {
    var that = this;
    GetList(that);
  },
  // 获取分类信息
  getCategoryInfo: function () {
    var that = this;
    var categoryId = that.data.categoryId;
    http.get('category', { act: 'info', categoryId: categoryId}).then(function (res) {
      if (res != false) {
        wx.setNavigationBarTitle({
          'title': res.name
        });
      }
    });
  },
  goToUrl: function (e) {
    var that = this;
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    })
  },
  // 获取设置
  getSetting: function () {
    var that = this;
    var cache = wx.getStorageSync("setting");
    if (cache) {
      that.setData({
        setting: cache
      });
      var backgroundColor = cache.backgroundColor != undefined ? cache.backgroundColor : '#000000';
      var frontColor = cache.frontColor != undefined ? cache.frontColor : '#ffffff';
      wx.setNavigationBarColor({
        frontColor: frontColor,
        backgroundColor: backgroundColor
      });
    } else {
      http.get('setting', {}).then(function (res) {
        if (res != {}) {
          wx.setStorageSync('setting', res);
          that.setData({
            setting: res
          });
          var backgroundColor = res.backgroundColor != undefined ? res.backgroundColor : '#000000';
          var frontColor = res.frontColor != undefined ? res.frontColor : '#ffffff';
          wx.setNavigationBarColor({
            frontColor: frontColor,
            backgroundColor: backgroundColor
          });
        } else {
          that.setData({
            setting: []
          });
        }
      });
    }
  }
})